package net.huashitong.zhApp.controller;

import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.BuildProjects;
import net.huashitong.Project.filter.BuildProjectsFilter;
import net.huashitong.Project.responseModel.BuildProjectsModel;
import net.huashitong.Project.service.BuildProjectsService;
import net.huashitong.user.entity.BnpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/zhApp/buildProject")
public class AppBuildProjectController extends BaseCtrl {
    @Autowired
    private BuildProjectsService buildProjectsService;


    /**
     * 查询在建项目（有分页）
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Map<String, Object> buildProjectList(@RequestBody BuildProjectsFilter filter) {
        BnpUser user = queryUserAuth(filter.getUserPhone());
        if (ObjectUtils.isBlank(user)) {
            return coverMessage("202", "无用户权限");
        }
        Map<String, Object> resultMap = buildProjectsService.queryBuildProjectsList(user, filter);
        //到时这里可以加时间进行判断什么时候可以上报进度
        List<BuildProjectsModel> list = (List<BuildProjectsModel>) resultMap.get("list");
        int pageCount = (int) resultMap.get("pageCount");
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "查询在建项目（有分页）");
        result.put("projects", list);
        result.put("count", pageCount);
        return result;
    }

    /**
     * 查看进度报告历史记录
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryHistoricalRecord", method = RequestMethod.POST)
    public Map<String, Object> queryHistoricalRecord(@RequestBody BuildProjectsFilter filter) {
        BnpUser user = queryUserAuth(filter.getUserPhone());
        if (ObjectUtils.isBlank(user)) {
            return coverMessage("202", "无用户权限");
        }
        List<BuildProjects> list = buildProjectsService.queryBuildProjectsByFilter(filter);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "查看进度报告历史记录（无分页）");
        result.put("projects", list);
        return result;
    }

    /**
     * 添加或者修改进度报告
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addBuildRecord", method = RequestMethod.POST)
    public Map<String, Object> addBuildRecord(@RequestBody BuildProjectsFilter filter, HttpServletRequest request) {
        BnpUser bnpUser = (BnpUser) WebUtils.getSessionAttribute(request, "user");
        BnpUser user = queryUserAuth(filter.getUserPhone());
        if (ObjectUtils.isBlank(user)) {
            return coverMessage("202", "无用户权限");
        }
        buildProjectsService.addBuildRecord(filter, bnpUser);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "添加或者修改进度报告");
        return result;
    }

    /**
     * 删除历史进度报告
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete/{id}/{userPhone}")
    public Map<String, Object> delete(@PathVariable List<String> id, @PathVariable String userPhone) {
        BnpUser user = queryUserAuth(userPhone);
        if (ObjectUtils.isBlank(user)) {
            return coverMessage("202", "无用户权限");
        }
        buildProjectsService.batchDelete(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "删除历史进度报告");
        return result;
    }
}
